import { Component, OnInit } from '@angular/core';

import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';

import { app } from '../application.data';
import { appUrl } from '../application.url';

@Component({
  selector: 'application-details',
  templateUrl: './application-details.component.html',
  styleUrls: ['./application-details.component.css']
})
export class ApplicationDetailsComponent implements OnInit {

  app : any = app;

  details              : object;
  grantTypeDisplayName : Array<string> = [];
  scopeDisplayName     : Array<any> = [];
  
  constructor(
    private request : CudTableService
  ) {}

  ngOnInit() {
  }

  /**
   * 详情
   * @param appId 
   */ 
  _details(appId) { 

    // 清空
    this.grantTypeDisplayName = [];
    this.scopeDisplayName     = [];

    let grantType : Array<string>;
    let scope     : Array<string>;

    this.request
        .postRequest(`${ appUrl.getAppsInfo }${ appId }`, '')
        .then(data=>{

          this.details = data;

          // 许可类型
          grantType = data.result.grantType.split(',');

          for(let i=0; i<data.authType.length; i++) {
            for(let j=0; j<grantType.length; j++) {
              if(data.authType[i].sysValue == grantType[j]) {
                this.grantTypeDisplayName.push(data.authType[i].displayName);
              }
            }
          }

          // 授权范围
          scope = data.result.scope.split(',');

          for(let i=0; i<data.scopes.length; i++) {

            this.scopeDisplayName.push({'serviceTypeName' : data.scopes[i].serviceTypeName, 'scopeName' : []});
            
            for(let j=0; j<data.scopes[i].scopesTypeVo.length; j++) {
              for(let k=0; k<scope.length; k++) {
                if(data.scopes[i].scopesTypeVo[j].scopesCode==scope[k]) {
                  this.scopeDisplayName[i].scopeName.push(data.scopes[i].scopesTypeVo[j].scopeName);
                }
              }
            }
          }
          
        })
        .catch(err=> {
          return
        });
  }

}
